package com.itextpdf.local.ess;

import com.itextpdf.local.ess.PDFLocation.LocationFactory;
import com.itextpdf.local.ess.PDFLocation.PDELocation;
import com.itextpdf.local.ess.Signature.ESSMakeSignature;
import com.itextpdf.local.ess.Signature.SealBuilder;
import com.itextpdf.local.ess.predefine.PictureType;
import com.itextpdf.local.ess.predefine.SealStandard;
import com.itextpdf.local.ess.predefine.SealStruct;


public class test {

    static byte[] pdfByte = FileUtils.File2byte("D:\\pdf\\test\\demo.pdf");
    static byte[] pfx = FileUtils.File2byte("D:\\pdf\\cert\\rootjs.pfx");
    static byte[] cer = FileUtils.File2byte("D:\\pdf\\cert\\rootjs.cer");
    static byte[] img = FileUtils.File2byte("D:\\pdf\\test\\demo.gif");
    static String saveFile = "D:\\pdf\\test\\demo1.pdf";

    public static void main(String[] args) {
        sigDemo1();
    }


    //sigDemo1
    public static void sigDemo1(){
        //坐标
        PDELocation location = new LocationFactory().getLocationXY(100,100,1);
        SealBuilder sealBuilder = new SealBuilder();
        SealStruct sealStruct= sealBuilder.SealId("1111111111")
                .SealName("测试章")
                .Img(42,42,img, PictureType.GIF)
                .SealStandard(SealStandard.ESSRSAPFX)
                .Pfx(pfx,"111111")
                .build();

        byte[] pdfByte1 = null;
        try {
            pdfByte1 = ESSMakeSignature.makeSignature(pdfByte,location,sealStruct);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        FileUtils.byte2File(pdfByte1,saveFile);
    }
//    //signDemo2
//    public static void signDemo2(){
//        //坐标
//        PDELocation location = new LocationFactory().getLocationXY(100,100,1);
//        SealBuilder sealBuilder = new SealBuilder();
//        SealStruct sealStruct= sealBuilder.SealId("1111111111")
//                .SealName("测试章")
//                .Img(42,42,img, PictureType.GIF)
//                .SealStandard(SealStandard.ESSRSAPFX)
//                .CertByte(cer)
//                .Uk(UKType.LM_GM3000,"11111")
//                .build();
//        try {
//            //步骤1
//            PreSignatureBody preSignatureBody =  ESSMakeSignature.preMakeSignature(pdfByte,location,sealStruct);
//
//            byte[] pdf = preSignatureBody.getPdfBytes();
//            byte[] hash = preSignatureBody.getHashBytes();
//            byte[] toSign = preSignatureBody.getToSignBytes();
//            String fieldName = preSignatureBody.getFieldName();
//
//            //步骤2计算签名值
//            byte[] signatureBytes  = RSACrypto.SignData(toSign,pfx,"111111");
//
//
//            //步骤3
//            PreSignatureBody preSignatureBody1 = new PreSignatureBody(fieldName,hash,signatureBytes);
//            byte[] signedPdf  = ESSMakeSignature.SignPreSignedPdf(pdf,sealStruct,preSignatureBody1);
//            FileUtils.byte2File(signedPdf,saveFile);
//        } catch (IOException | DocumentException | GeneralSecurityException e) {
//            e.printStackTrace();
//        }
//    }

    //location demo
    public static void locationDemo(){
        //坐标
        PDELocation location = new LocationFactory().getLocationXY(100,100,1);
        //关键字 全文检索
        PDELocation location1 = new LocationFactory().getLocationKeyword(pdfByte,"煤炭合同",1);
        //关键字 指定页检索
        PDELocation location2 = new LocationFactory().getLocationKeyword(pdfByte,"煤炭合同",1,1);
        //书签
        PDELocation location3 = new LocationFactory().getLocationBookMark(pdfByte,"sign");
    }
}
